package com.liu.es.listener;

import com.liu.es.services.impl.GoodsServiceImpl;
import com.liu.shop.common.constant.RabbitConstant;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class EsQueueListener {
    @Autowired
    private GoodsServiceImpl goodsService;

    /**
     * 监听商品模块是否成功添加了商品后发布了消息到mq中
     * @param goodsVoJSON
     * @param channel
     * @param message
     */
    @RabbitListener(queues = RabbitConstant.GOODS_TOES_QUEUE)
    public void saveGoodsToEs(String goodsVoJSON, Channel channel, Message message) {

        try {
            // 2.插入到es中
            goodsService.addGoods(goodsVoJSON);
            // 手动ack，成功消费得到响应后，删除MQ消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
